home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
prolog
/
ai.prl
/
opnprlg1.hqx
/
Open Prolog
/
Samples
/
puzzle
next >
Wrap
Text File
|
1993-03-15
|
862b
|
22 lines
%simple puzzle: pick a number with all nine digits 1,2,3,4,5,6,7,8,9 in it exactly once,
%such that the first k digits in the number are divisible by k, for k from 1 to 9.
%call: stageDiv("123456789",1,[],Result).
stageDiv([],Divisor,Result,NewNumber) :- %finished - just generate result number from string
name(NewNumber,Result).
stageDiv(Digits,Divisor,DigitListSoFar,Result) :-
select(NewDigit,Digits,RestOfDigits), %pick some digit
append(DigitListSoFar,[NewDigit],NewDigitList),
name(NewNumber,NewDigitList), %turn it into a number
0 is NewNumber mod Divisor, %check against next divisor
NewDivisor is Divisor+1,
stageDiv(RestOfDigits,NewDivisor,NewDigitList,Result). %do the rest
append([],X,X).
append([X|Y],Z,[X|R]) :- append(Y,Z,R).
select(X,[X|R],R).
select(X,[Y|R],[Y|Z]) :- select(X,R,Z).